package cn.cdzdjd.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.springframework.stereotype.Service;

import cn.cdzdjd.exception.ErrorCode;
import cn.cdzdjd.exception.RenException;
import cn.cdzdjd.service.MeiPaiService;
import cn.cdzdjd.util.JsoupUtil;
import cn.cdzdjd.util.UrlUtil;
import cn.cdzdjd.vo.VideoInfoVO;

@Service
public class MeiPaiServiceImpl implements MeiPaiService {

	@Override
	public VideoInfoVO getVideoInfo(String url) {
		try {
			Document document = JsoupUtil.getDocument(url);
			Element body = document.body();
			String videoTitle = document.selectFirst("meta[name=description]").attr("content");
			String videoUrl_base64 = body.selectFirst("div[id=detailVideo]").attr("data-video");
			String videoUrl = UrlUtil.decodeVideoUrl(videoUrl_base64);
			String coverUrl = body.selectFirst("img[itemprop=image]").attr("src");
			List<String> videoList = new ArrayList<>();
			videoList.add(UrlUtil.setHttps(videoUrl));
			List<String> posterList = new ArrayList<>();
			posterList.add(UrlUtil.setHttps(coverUrl));
			return new VideoInfoVO(videoList, posterList, videoTitle, null);
		} catch (Exception e) {
			e.printStackTrace();
			throw new RenException(ErrorCode.PARSE_ERROR);
		}
	}

}
